package thread;

/**
 * 静态方法如果使用synchronized修饰, 那么该方法一定具有同步效果, 所得都只能是当前类
 */
public class SyncDemo2 {
    public static void main(String[] args) {
        /*
        // 匿名类方法
        Thread t1 = new Thread(new Runnable){
            public void run(){
                Boo.doSome();
            }
        };
         */

        /*
        // lambda表达式
        Thread t1 = new Thread(()->{
            Boo.doSome();
        });
        */

        //方法引用
        Thread t1 = new Thread(Boo::doSome);
        Thread t2 = new Thread(Boo::doSome);

        t1.start();
        t2.start();
    }
}


class Boo{
    //public static synchronized void doSome(){ //
    public static void doSome(){
        synchronized (Boo.class){//静态方法, 同步监视对象是Boo.class, Boo.class为当前类
            try {
                Thread t = Thread.currentThread();
                System.out.println(t.getName() + ":正在执行doSome方法");
                Thread.sleep(3000);
                System.out.println(t.getName() + ":执行完毕doSome方法");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}